<?php
/**
 * Created by PhpStorm.
 * User: wo
 * Date: 2019/9/18
 * Time: 17:33
 */
class Wish_templateApp extends BackendApp
{
    protected $_wish_template;

    function __construct()
    {
        $this->Banner_settingApp();
    }

    function Banner_settingApp()
    {
        parent::BackendApp();
        $this->_wish_template = &m('wish_template');
    }

    //模板列表
    function index(){
        $page = $this->_get_page(10);
        $res = $this->_wish_template->find(array(
            'conditions'    => '1=1',
            'limit' => $page['limit'],
            'count' => true,
        )); //找出所有商城的合作伙伴

        $page['item_count'] = $this->_wish_template->getCount();   //获取统计的数据
        $this->_format_page($page);
        $this->assign('res', $res);
        $this->assign('filtered', $conditions? 1 : 0); //是否有查询条件
        $page['item_count'] = $this->_wish_template->getCount();
        $this->import_resource(array('script' => 'inline_edit.js,jquery.ui/jquery.ui.js,jquery.ui/i18n/' . i18n_code() . '.js',
            'style'=> 'jquery.ui/themes/ui-lightness/jquery.ui.css'));
        $this->_format_page($page);
        $this->assign('page_info', $page);
        $this->display('wish_template.index.html');
    }

    //添加模板
    public function add(){
        if(IS_POST){
            $data = array(
                'assist_num' => $_POST['assist_num'],
                'assist_money'=>$_POST['assist_money'],
                'firstphase_num_percentage' => $_POST['firstphase_num_percentage'],
                'firstphase_money_percentage' => $_POST['firstphase_money_percentage'],
                'secondphase_num_percentage' => $_POST['secondphase_num_percentage'],
                'secondphase_money_percentage' => $_POST['secondphase_money_percentage'],
                'thirdphase_num_percentage' => $_POST['thirdphase_num_percentage'],
                'thirdphase_money_percentage' => $_POST['thirdphase_money_percentage'],
                'float_percentage' => $_POST['float_percentage'],
                'plus_minus' => $_POST['plus_minus']
            );
            if(!is_numeric($data['assist_num']) || $data['assist_num'] < 10 || !preg_match("/^[1-9][0-9]*$/",$data['assist_num'])){
                $this->show_warning('帮砍人数不可以小于10或格式错误');
                return;
            }
            if($data['assist_money'] < 1){
                $this->show_warning('帮砍金额不可小于1');
                return;
            }
            if($data['firstphase_num_percentage'] >= 1 || $data['firstphase_money_percentage'] >= 1 || $data['secondphase_num_percentage'] >= 1 || $data['secondphase_money_percentage'] >= 1 || $data['thirdphase_num_percentage'] >= 1 || $data['thirdphase_money_percentage'] >= 1 || $data['float_percentage'] >= 1){
                $this->show_warning('百分比数值不可大于1');
                return;
            }

            if($data['plus_minus'] < 0){
                $this->show_warning('正负值不可以小于0');
                return;
            }

            $totalNum = $data['firstphase_num_percentage'] + $data['secondphase_num_percentage'] + $data['thirdphase_num_percentage'];
            $totalMoney = $data['firstphase_money_percentage'] + $data['secondphase_money_percentage'] + $data['thirdphase_money_percentage'];

            if($totalMoney < 1 || $totalNum < 1){
                $this->show_warning('百分比数值没达到100%,请检查后提交！');
                return;
            }
            if($totalNum > 1 || $totalMoney > 1){
                $this->show_warning('百分比数值已超过100%,请检查后提交！');
                return;
            }

            $info = $this->_wish_template->add($data);
            if ($this->_wish_template->has_error()){
                $this->show_warning($this->_wish_template->get_error());
                return;
            }else{
                $this->admin_log("添加心愿单模板失败",1);
            }
            $this->show_message('添加成功',
                'back_list',        'index.php?app=wish_template',
                'continue_add',    'index.php?app=wish_template&amp;act=add');

        }else{
            $this->display('wish_template.form.html');
        }
    }

    function view(){
        $template_id = isset($_GET['template_id']) ? intval($_GET['template_id']) : 0;
        if(IS_POST){
            $data = array(
                'assist_num' => $_POST['assist_num'],
                'assist_money'=>$_POST['assist_money'],
                'firstphase_num_percentage' => $_POST['firstphase_num_percentage'],
                'firstphase_money_percentage' => $_POST['firstphase_money_percentage'],
                'secondphase_num_percentage' => $_POST['secondphase_num_percentage'],
                'secondphase_money_percentage' => $_POST['secondphase_money_percentage'],
                'thirdphase_num_percentage' => $_POST['thirdphase_num_percentage'],
                'thirdphase_money_percentage' => $_POST['thirdphase_money_percentage'],
                'float_percentage' => $_POST['float_percentage'],
                'plus_minus' => $_POST['plus_minus']
            );
            if(!is_numeric($data['assist_num']) || $data['assist_num'] < 10 || !preg_match("/^[1-9][0-9]*$/",$data['assist_num'])){
                $this->show_warning('帮砍人数不可以小于10或格式错误');
                return;
            }
            if($data['assist_money'] < 1){
                $this->show_warning('帮砍金额不可小于1');
                return;
            }
            if($data['firstphase_num_percentage'] >= 1 || $data['firstphase_money_percentage'] >= 1 || $data['secondphase_num_percentage'] >= 1 || $data['secondphase_money_percentage'] >= 1 || $data['thirdphase_num_percentage'] >= 1 || $data['thirdphase_money_percentage'] >= 1 || $data['float_percentage'] >= 1){
                $this->show_warning('百分比数值不可大于1');
                return;
            }

            if($data['plus_minus'] < 0){
                $this->show_warning('正负值不可以小于0');
                return;
            }

            $totalNum = $data['firstphase_num_percentage'] + $data['secondphase_num_percentage'] + $data['thirdphase_num_percentage'];
            $totalMoney = $data['firstphase_money_percentage'] + $data['secondphase_money_percentage'] + $data['thirdphase_money_percentage'];

            if($totalMoney < 1 || $totalNum < 1){
                $this->show_warning('百分比数值没达到100%,请检查后提交！');
                return;
            }
            if($totalNum > 1 || $totalMoney > 1){
                $this->show_warning('百分比数值已超过100%,请检查后提交！');
                return;
            }

            $info = $this->_wish_template->edit($template_id,$data);
            if ($this->_wish_template->has_error()){
                $this->show_warning($this->_wish_template->get_error());
                return;
            }else{
                $this->admin_log("编辑心愿单模板失败",1);
            }
            $this->show_message('保存成功',
                'back_list',        'index.php?app=wish_template',
                '再次编辑',    'index.php?app=wish_template&amp;act=view&template_id='.$template_id);
        }else{
            $info = $this->_wish_template->get(array('conditions'=>'template_id = '.$template_id));
            $this->assign('setting',$info);
            $this->display('wish_template.form.html');
        }
    }

    function drop(){
        $template_id = isset($_GET['template_id']) ? trim($_GET['template_id']) : '';
        if (!$template_id) {
            $this->show_warning('no_such_banner');
            return;
        }
        if (!$this->_wish_template->drop($template_id)) {    //删除
            $this->show_warning($this->_wish_template->get_error());
            return;
        }else{
            $this->admin_log('删除心愿单模板', 1);
        }

        $this->show_message('drop_ok');

    }
}